package com.example.mform.service.impl;

import com.example.mform.entity.Question;
import com.example.mform.entity.UserForm;
import com.example.mform.repository.UserFormRepository;
import com.example.mform.service.UserFormService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author shishuqian
 * date 2020/11/24
 * time 14:43
 **/
@Service
public class UserFormServiceImpl implements UserFormService {

    @Autowired
    private UserFormRepository userFormRepository;

    @Override
    public List<UserForm> findAll() {
        return userFormRepository.findAll();
    }

    @Override
    public Page<UserForm> findByPage(Integer page, Integer size) {
        Pageable pageable = PageRequest.of(page-1,size);
        return userFormRepository.findAll(pageable);
    }

    @Override
    public UserForm findById(Long id) {
        return userFormRepository.findById(id).get();
    }

    @Override
    public void save(UserForm userForm) {
        userFormRepository.save(userForm);
    }

    @Override
    public void update(UserForm userForm) {
        userFormRepository.save(userForm);
    }

    @Override
    public void deleteById(Long id) {
        userFormRepository.deleteById(id);
    }

    @Override
    public void release(Long id) {
        //根据表单id获取表单
        UserForm userForm = userFormRepository.findById(id).get();
        //如果此表单已发布
        if(userForm.getStatus()){
            userForm.setStatus(false);
            userFormRepository.save(userForm);
        }else{   //表单未发布
            userForm.setStatus(true);
            userFormRepository.save(userForm);
        }

    }
}
